package com.deepexps.security.commen;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

/**
 * 返回客户端的数据容器
 */
public class ResponseResult implements Serializable{

	private static final long serialVersionUID = 63683496470615211L;
	private int code;
	private String msg;
	private Object data;
	public ResponseResult(int code, String msg){
		this.code=code;
		this.msg=msg;
	}
	public ResponseResult(int code, String msg, Object data){
		this.code=code;
		this.msg=msg;
		this.data=data;
	}
	public static ResponseResult getSuccess(){
		return getSuccess(Message.HTTP_SERVER_OK,null);
	}
	public static ResponseResult getSuccess(Object data){
		return getSuccess(Message.HTTP_SERVER_OK,data);
	}
	public static ResponseResult getSuccess(Message message,Object data){
		return new ResponseResult(message.getCode(),message.getMsg(),data);
	}
	public static ResponseResult getError(){
		return getError(Message.HTTP_SERVER_ERROR,null);
	}
	public static ResponseResult getError(Message message){
		return getError(message,null);
	}
	public static ResponseResult getError(Message message,Object data){
		HttpServletResponse response=((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
		response.setStatus(message.getCode());
		return new ResponseResult(message.getCode(),message.getMsg(),data);
	}
	public int getCode() {
		return code;
	}
	public String getMsg() {
		return msg;
	}
	public void setData(Object data) {
		this.data = data;
	}
	public Object getData() {
		return data;
	}
}
